package com.example.borui.partpositionproject.partpisition.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.example.borui.partpositionproject.partpisition.base.BasePartPosition;
import com.example.borui.partpositionproject.partpisition.base.IConfig;
import com.example.borui.partpositionproject.partpisition.base.ItemViewType;
import com.example.borui.partpositionproject.partpisition.view.IPartPositionBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by borui on 2017/9/29.
 */
public class PartPistionAdapterHelper {

    private List<BasePartPosition> mHomeProcessList;

    private IPartInterface<Integer> iPartInterface;

    public PartPistionAdapterHelper(IPartInterface<Integer> iPartInterface) {
        this.iPartInterface = iPartInterface;
        mHomeProcessList = new ArrayList<>();
    }

    public RecyclerView.ViewHolder onCreateViewHolder(int viewType) {
        // 这个时候的 viewType 就是position 获取 区域类型
        int position = viewType / 10000; // 分区位置
        int type = viewType % 10000;   // view 的type
        BasePartPosition baseHomeProcessor = getProcessorByAreaPosition(position);
        if (null != baseHomeProcessor) {
            return baseHomeProcessor.createViewHolder(type);
        }
        return null;
    }

    public int getItemViewType(int position) {
        int areaPostion = iPartInterface.getItem(position);
        BasePartPosition baseHomeProcessor = getProcessorByAreaPosition(areaPostion);
        if (null != baseHomeProcessor) {
            int viewType = baseHomeProcessor.getItemViewType(position - baseHomeProcessor.getStartCount());
            return viewType + areaPostion * 10000;
        }
        return position;
    }

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Integer areaPostion = iPartInterface.getItem(position);
        BasePartPosition baseHomeProcessor = getProcessorByAreaPosition(areaPostion == null ? 0 : areaPostion);
        if (null != baseHomeProcessor) {
            /***
             * @params holder
             * @params areapPsition  分区中的位置
             */
            baseHomeProcessor.onBindViewHolder(holder, position - baseHomeProcessor.getStartCount());
            if(null != iPartInterface){
                iPartInterface.onBindPartPosition(baseHomeProcessor, areaPostion, position);
            }
        }
    }

    /***
     * 添加分区
     *
     * @param iProcessor
     */
    protected void addProcessor(BasePartPosition iProcessor) {
        if (null == mHomeProcessList) {
            mHomeProcessList = new ArrayList<>();
        }
        mHomeProcessList.add(iProcessor);
    }

    /***
     * 添加分区
     *
     * @param iProcessors
     */
    protected void addAllProcessor(List<BasePartPosition> iProcessors) {
        if (null == mHomeProcessList) {
            mHomeProcessList = new ArrayList<>();
        }
        mHomeProcessList.addAll(iProcessors);
    }

    /***
     * 添加分区
     *
     * @param iProcessors
     */
    protected void updataProcessors(List<BasePartPosition> iProcessors) {
        if (null == mHomeProcessList) {
            mHomeProcessList = new ArrayList<>();
        }else{
            mHomeProcessList.clear();
        }
        mHomeProcessList.addAll(iProcessors);
    }

    protected BasePartPosition getProcessorByAreaPosition(int position) {
        if (null == mHomeProcessList) {
            mHomeProcessList = new ArrayList<>();
        }

        if (position < mHomeProcessList.size()) {
            return mHomeProcessList.get(position);
        }

        return null;
    }

    public BasePartPosition getLastProcessor() {
        if (null != mHomeProcessList && mHomeProcessList.size() > 0) {
            return mHomeProcessList.get(mHomeProcessList.size() - 1);
        }
        return null;
    }

    protected BasePartPosition buildProcessor(int position, IPartPositionBean bean){
        return iPartInterface.buildProcessor(position, bean);
    }

    public void updatePositionItems(List<IPartPositionBean> mDatas) {
        clearProcessor();
        if (mDatas == null) {
            return;
        }
        this.addPositionItems(mDatas);
    }

    public void clearProcessor() {
        iPartInterface.clear();
        this.mHomeProcessList.clear();
        iPartInterface.notifyDataSetChanged();
    }

    public void addPositionItem(IPartPositionBean mData) {
        if (mData == null) return;

        List<IPartPositionBean> mDatas = new ArrayList<>();
        mDatas.add(mData);

        this.addPositionItems(mDatas);
    }

    public void addPositionItems(List<IPartPositionBean> mDatas) {
        if (mDatas == null) return;

        int startCount = iPartInterface.getItemCount();

        int startPosition = 0;
        int lastEndItemType = 0;

        /***
         * 循环创建分区
         */
        for (int i = startCount; i < mDatas.size() + startCount; i++) {
            BasePartPosition baseHomeProcessor = buildProcessor(i, mDatas.get(i - startCount));
            System.out.println("wo shi shen " + iPartInterface.logName());
            if (null != baseHomeProcessor) {
                baseHomeProcessor.updataBean(mDatas.get(i - startCount));
                // 加10 就是防止 重复 没有别的含义
                baseHomeProcessor.setPartStartItemType(lastEndItemType + ItemViewType.START_POSITION);
                baseHomeProcessor.setStartCount(startPosition);
                // 添加 分区
                addProcessor(baseHomeProcessor);
                // 添加 分区占位数据
                iPartInterface.addItems(baseHomeProcessor.getList());
                startPosition += baseHomeProcessor.getList().size();
            }
        }
        iPartInterface.notifyDataSetChanged();
    }

    public void notifyProcessAdapter() {
        iPartInterface.clear();
        for (BasePartPosition baseHomeProcessor : mHomeProcessList) {
            // 添加 分区占位数据
            iPartInterface.addItems(baseHomeProcessor.getList());
        }
        iPartInterface.notifyDataSetChanged();
    }

    public BasePartPosition getProcessor(int position){
        Integer areaPosition = iPartInterface.getItem(position);
        if(null != areaPosition && areaPosition < mHomeProcessList.size()){
            return mHomeProcessList.get(areaPosition);
        }
        return null;
    }

    public int getSpanCount(int position) {
        Integer areaPostion = iPartInterface.getItem(position);
        BasePartPosition baseHomeProcessor = getProcessorByAreaPosition(areaPostion == null ? 1 : areaPostion);
        if (null != baseHomeProcessor) {
            return baseHomeProcessor.getSpanCount(position - baseHomeProcessor.getStartCount());
        }
        return IConfig.CLUMS;
    }

    public List<BasePartPosition> getHomeProcessList() {
        return mHomeProcessList;
    }

}
